const appId = 'wx7bc96924b8f68fbd'
const appsecret = 'c9511bb86acb8a21c6dd783a678c0837'
const axios = require('axios')
const sign = require('./sign.js')
/**
 * access_token: https请求方式: GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
 * 临时票据GET ：https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi
 */

// 老师这里没有请求失败校验
 
// promise版本
/* promiseWechatSkd()
function promiseWechatSkd() {
  // 1. 通过公众号id和公众号秘钥获取access_token
  axios.get(`https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appId}&secret=${appsecret}`)
  .then(result => {
    // console.log(result.data) // {access_token: 'dfgfdg'}
    const {access_token} = result.data
    // 2. 通过access_token获取jsapi_ticket临时票据
    return axios.get(`https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=${access_token}&type=jsapi`)
  })
  .then(result => {
    // console.log(result.data)
    const {ticket} = result.data
    // sign(jsapi_ticket, url)
    const sdkResult = sign(ticket, 'localhost:3000/')
    console.log(sdkResult)
  })
} */



// async await版本
promiseWechatSkd()
async function promiseWechatSkd() {
  // 1. 通过公众号id和公众号秘钥获取access_token
  let result1 =  await axios.get(`https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appId}&secret=${appsecret}`)
  const {access_token} = result1.data
  // 2. 通过access_token获取jsapi_ticket临时票
  let result2 = await axios.get(`https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=${access_token}&type=jsapi`)
  const {ticket} = result2.data
  const sdkResult = sign(ticket, 'localhost:3000/')
  console.log(sdkResult)
}
